home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5973 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Initialising structure members - help please!
  5. Date: 21 Feb 1996 09:42:43 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gflijINN8hi@keats.ugrad.cs.ubc.ca>
  8. References: <4gb8hn$3m8@news.mistral.co.uk>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4gb8hn$3m8@news.mistral.co.uk>,
  12. Mike Barnard <mikebarnard@mistral.co.uk> wrote:
  13.  >Hi all.
  14.  >
  15.  >I'm a learner. I am trying to create a function to display a list of
  16.  >menu items using a doubly linked list. My problem is in initialising
  17.  >the members of the individual menu items.
  18.  >
  19.  >Within a function called VPCMENU.C I have created a structure...
  20.  >
  21.  >// Define a structure to hold the menu item information.
  22.  
  23. Standard C has no operator '//'. That is a syntax error. I think you want the
  24. "comp.lang.c++" newsgroup down the hall to the left!
  25.  
  26.  >struct    menuitem
  27.  >    {
  28.  >    int number;            // Item 1, item 2 etc.
  29.  >    char description[70];        // Room for a text description
  30.  >    struct menuitem *next;        // Pointer to next menu item
  31.  >    struct menuitem *last;        // Pointer to last menu item
  32.  >    };
  33.  >
  34.  >Now I create 5 instances of variables of the type menuitem...
  35.  >
  36.  >// Define the instances of the structure.
  37.  >
  38.  >struct menuitem one,two,three,four,five;
  39.  >
  40.  >Now, the fun (not) bit. I tried to initialise the items with...
  41.  >
  42.  >one.number = 1;
  43.  >one.description = "1.   Calculate a minefield";
  44.  
  45. That would work if description was a "char *", or if you performed the
  46. assignment in an declaration with an initializer rather than in a program
  47. statement:
  48.  
  49.     struct foo {
  50.         char data[20];
  51.         int num;
  52.     } my_struct = { "Bar", 1 };
  53.  
  54. This is only allowed (by some old compilers) if this is outside of a function,
  55. or preceded by a 'static' keyword. Otherwise you attempt something called
  56. "initialization of automatic aggregates", which is a no-no with these venerable
  57. old fogies..  However, ANSI allows such initialization ``...only by constant
  58. constructions unless the initializer can be expressed by a simple expression''.
  59. (K&R2 A8.7).
  60.  
  61.  >one.next = two;
  62.  >one.last = five;
  63.  >
  64.  >The first one passes OK, but the second one creates an error. As it
  65.  >stands it says "Lvalue required". Help says "The left side of an
  66.  >assignment operator must be an addressable expression." I don't know
  67.  >about the third and fourth ones. Yet.
  68.  
  69. Your compiler's message is not strictly correct. The left side of the
  70. expression _is_ an lvalue, because it refers to an object, and it is
  71. addressable. But it is not a _modifiable_ lvalue. Like an array, a function is
  72. also an lvalue, but it isn't modifiable:
  73.  
  74.     [Assignment Expressions] require an lvalue as a left operand, and the
  75.     lvalue must be modifiable: it must not be an array, and must not have
  76.     an incomplete type, or be a function. (K&R2 A7.17)
  77.  
  78.  >This is an almost exact copy of the structure examples in my book.
  79.  >(The beginners guide to C by Wrox). Page 387 for those who have it.
  80.  
  81. How exact is "almost" exact? Does the Wrox book also try to do an assignment to
  82. an array variable?
  83.  
  84. Perhaps you might avail yourself of a copy of the Kernighan and Ritchie text.
  85. It is very useful companion. Also, go to the FTP site rtfm.mit.edu (or a
  86. suitable mirror), and look for the comp.lang.c FAQ in /pub/usenet
  87.  
  88.  >So, can anyone help please? (I must get snippets!). This is for a PC
  89.  >in text mode using Borland C++ 3.0. The completed program is to do
  90.  
  91. Again, this is the comp.lang.c newsgroup, not C++. A strictly standard
  92. compiler's pre-processor won't even accept your style of writing comments.
  93. -- 
  94.  
  95.